import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { TurbstatusEditComponent } from '../turbstatus-edit/turbstatus-edit.component';
import { TurbstatusCheckComponent } from '../turbstatus-check/turbstatus-check.component';
import { BladeContainerBase } from '../../../../shared/blade/blade-container-base';
import { BladeData } from '../../../../shared/blade/blade-data';
import { Blade } from '../../../../shared/blade/blade.model';
import { Command } from '../../../../shared/toolbar.service';
import { TurbStatus } from '../../../../entity/turbstatus.model';
import { TurbStatusService } from '../../../../service/turbstatus.service';
import { DialogResult } from '../../../../shared/dialog.service';

@Component({
    selector: 'app-turbstatus-list',
    templateUrl: './turbstatus-list.component.html',
    styleUrls: ['./turbstatus-list.component.css']
})
export class TurbstatusListComponent extends BladeContainerBase<TurbStatus> {
    private turbStatuses: TurbStatus[] = [];
    private originalTurb: TurbStatus[] = [];

    private inverterStatuses: TurbStatus[] = [];
    private originalInverter: TurbStatus[] = [];

    public deviceStatusesList: TurbStatus[] = [];


    constructor(
        private turbastatusService: TurbStatusService,
    ) {
        super();
    }
    // tslint:disable-next-line:use-life-cycle-interface
    ngOnInit(): void {
        this.commands = [
            new Command('add', '添加', 'fa fa-plus', 'setofkey:insert', blade => this.exeAdd(blade), blade => this.canExeAdd(blade)),
            new Command('edit', '编辑', 'fa fa-edit', 'setofkey:update', blade => this.exeEdit(blade), blade => this.canExeEdit(blade)),
            new Command('delete', '删除', 'fa fa-trash-o', 'setofkey:delete', blade => this.exeDelete(blade), blade => this.canDelete(blade))
        ];
        super.ngOnInit();
    }
    /**
     * 初始化数据
     *
     *
     * @memberof TurbstatusListComponent
     */
    initData(): any | Observable<any> | Promise<any> {
        this.deviceStatusesList = [];
        const turbStatues = this.turbastatusService.getTurbStatus();
        const inverterStatues = this.turbastatusService.getInverterStatus();
        Promise.all([turbStatues, inverterStatues])
            .then(res => {
                if (res[0] && res[1]) {
                    this.originalTurb = [...res[0]];
                    this.turbStatuses = [...this.originalTurb];
                    this.originalInverter = [...res[1]];
                    this.inverterStatuses = [...this.originalInverter];
                    this.deviceStatusesList = [...this.turbStatuses, ...this.inverterStatuses];
                } else {
                    throw new Error('查询失败！');
                }
            })
            .catch(error => this.bladeData.error = error)
            .then(res => this.bladeData.isLoading = false);
    }
    exeAdd(blend: Blade): void {
        const data: BladeData = new BladeData();
        data.id = 'turbaKeyCreate';
        data.title = '设备状态';
        data.subtitle = '创建一个设备状态';
        data.isClosingDisabled = false;
        const createKeyBlade = this.bladeService.createBlade(TurbstatusEditComponent, data, true);
        this.bladeService.showBlade(createKeyBlade, this.currentBlade);
    }

    canExeAdd(blend: Blade): boolean {
        return true;
    }
    exeEdit(blend: Blade): void {
        if (this.currentEntity != undefined) {

            const data: BladeData = new BladeData();
            data.id = 'turbaKeyEdit';
            data.title = this.currentEntity.statusName;
            const editKeyBlade = this.bladeService.createBlade(TurbstatusEditComponent, data, false, this.currentEntity);
            this.currentBlade.bladeService.showBlade(editKeyBlade, this.currentBlade);
        }
    }
    canExeEdit(blend?: Blade): boolean {
        return this.currentEntity != null;
    }
    exeDelete(blend: Blade): void {
        const selectedKeys = this.getSelected();
        if (selectedKeys.length > 0) {
            this.dialogService
                .showConfirmationDialog('确认删除', '是否要删除选中项？')
                .subscribe(dialogResult => {
                    if (dialogResult == DialogResult.Ok) {
                        this.currentBlade.bladeData.isLoading = true;
                        this.turbastatusService.batchDelete(selectedKeys.map(o => o.statusId))
                            .then(res => {
                                if (res) {
                                    this.refresh(undefined);
                                }
                            })
                            .catch(error => this.bladeData.error = error)
                            .then(res => this.bladeData.isLoading = false);
                    }
                });
        }
    }
    canDelete(blend?: Blade): boolean {
        return this.getSelected().length > 0;
    }

    /**
     * 单选
     *
     * @param {any} event
     * @param {any} key
     *
     * @memberof DicKeyListComponent
     */
    onItemCheack(event: HTMLInputElement, key: TurbStatus) {
        key.isChecked = event.checked;
    }
    /**
     * 全选/全不选
     *
     * @param {HTMLInputElement} event
     *
     * @memberof DicKeyListComponent
     */
    onAllCheack(event: HTMLInputElement) {
        if (this.turbStatuses.length > 0) {
            for (let i = 0; i < this.turbStatuses.length; i++) {
                this.turbStatuses[i].isChecked = event.checked;
            }
        }
    }
    onSeletedChange(selectedKey: TurbStatus) {
        this.currentEntity = selectedKey;
        /* if (this.currentBlade.childrenCanDeactivate()) {
            this.showTurbstatusCheckBlade(selectedKey);
        } */
    }
    /**
     * 展开风机状态列表弹出层
     *
     * @param {TurbStatus} selectedKey
     *
     * @memberof TurbStatusListComponent
     */
    showTurbstatusCheckBlade(selectedKey: TurbStatus) {
        if (this.currentEntity != selectedKey
            || this.bladeService.findBlade('turbstatusCheckBlade') == undefined) {
            const data: BladeData = new BladeData();
            data.id = 'turbstatusCheckBlade';
            data.title = selectedKey.statusName;
            data.isClosingDisabled = false;
            const turbstatusCheckBlade = this.bladeService.createBlade(TurbstatusCheckComponent, data, false, selectedKey);
            this.bladeService.showBlade(turbstatusCheckBlade, this.currentBlade).then(res => {
                if (res) {
                    this.currentEntity = selectedKey;
                }
            });

        }
    }
    /**
     * 获得选中项
     *
     * @returns {Setofkey[]}
     *
     * @memberof DicKeyListComponent
     */
    getSelected(): TurbStatus[] {
        if (this.turbStatuses.length > 0) {
            const filter = this.turbStatuses.filter(ref => ref.isChecked);
            return filter == null ? [] : filter;
        }
        return [];
    }
}



// WEBPACK FOOTER //
// ./src/app/module/basic/turbstatus/turbstatus-list/turbstatus-list.component.ts